//
//  PlanetScene.cpp
//  SpaceTrader
//
//  Created by Brandon Chastain on 10/17/12.
//  Copyright (c) 2012 The Hex Pistols. All rights reserved.
//

#include "PlanetScene.h"
#include <iostream>
#include <cstring>
#include <string>
#include <sstream>

/*
 * draws the scene and handles its logic
 */
state_t PlanetScene::draw(SDL_Surface *screen, Planet *currPlanet, Player *player)
{
	int x1 = 100;
    if (currPlanet== NULL){
        std::cerr << "Current planet is null." <<std::endl;
        return QUIT;
    }
	SDL_Surface *infobox = load_image("infobox.png", false);
    SDL_Surface *background = load_image("planet.jpg", false);

    SDL_Event event;
	state_t next_state = PLANET;
    
	//Initialize Objects
	GuiButton mapButton(0,0,"", "Map");
    GuiButton marketButton(0, 374, "", "Market");
	GuiButton commonsButton(0, 427, "", "Commons");
    GuiButton shipyardButton(0, 321, "", "Shipyard");
    GuiButton saveButton(0, 268, "", "Save");
    
    GuiTable infoTable(4, 2, 0, 0, 110, 20, 16);
    std::vector<std::string> info = std::vector<std::string>();
    info.push_back("Location:");
    info.push_back("Tech Level:");
    info.push_back("Resources:");
	info.push_back("World Event:");
    info.push_back(currPlanet->getName());
    info.push_back(currPlanet->techLevelToString());
    info.push_back(currPlanet->resourcesLevelToString());
	info.push_back(currPlanet->worldEventToString());
    infoTable.setContents(info);
    infoTable.setCoords(screen->w-infoTable.getTotalWidth()-5, 0);
    
    GuiTable shipInfo(1, 2, 0, 0, 90, 20, 16);
    std::ostringstream out;
    out << player->getFuel();
    std::vector<std::string> info1;
    info1.push_back("Ship Fuel:");
    info1.push_back(out.str().c_str());
    shipInfo.setContents(info1);
    shipInfo.setCoords(screen->w-shipInfo.getTotalWidth()-5, 460);

	while (next_state==PLANET) {
		
		//Event Loop
		while (SDL_PollEvent(&event)) {
			//Buttons
			if (mapButton.handle_input(event) == CLICKED) next_state = MAP;
            if (marketButton.handle_input(event) == CLICKED) next_state = MARKET;
			if (commonsButton.handle_input(event) == CLICKED) next_state = COMMONS;
            if (shipyardButton.handle_input(event) == CLICKED) next_state = SHIPYARD;
            if (saveButton.handle_input(event) == CLICKED) next_state = SAVE;
			if (event.type==SDL_QUIT) next_state = QUIT;
		}
		//Apply objects
        SDL_FillRect(screen, &screen->clip_rect, SDL_MapRGB(screen->format, 0x00, 0x00, 0x00));
        apply_surface(75, 0, background, screen);
		apply_surface(410+x1, -10+x1, infobox, screen);
		apply_surface(490, 450+x1, infobox, screen);
		if(x1>0) x1-=5;
		mapButton.show(screen);
        marketButton.show(screen);
        infoTable.show(screen);
        shipInfo.show(screen);
		commonsButton.show(screen);
        shipyardButton.show(screen);
        saveButton.show(screen);
        
        
		//Update the screen
		if ( SDL_Flip( screen ) == -1 ) {
			std::cerr << "Error Updating screen." << std::endl;
		}
	}
	SDL_FreeSurface(background);
	
	return next_state;
}
